package org.gdufe.bookCollections.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.gdufe.bookCollections.mapper.BookCollectionMapper;
import org.gdufe.bookCollections.service.BookCollectionService;
import org.gdufe.pojos.dto.AddCollectionDto;
import org.gdufe.pojos.dto.DeleteCollectionDto;
import org.gdufe.pojos.dto.PageParams;
import org.gdufe.pojos.pojo.Book;
import org.gdufe.pojos.pojo.BookCollection;
import org.gdufe.pojos.vo.PageResponseResult;
import org.gdufe.pojos.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class BookCollectionServiceImpl implements BookCollectionService {
    @Autowired
    private BookCollectionMapper bookCollectionMapper;

    @Override
    public PageResponseResult querry(PageParams pageParams, String username) {
        if (pageParams==null||pageParams.getPageNo()<=0||pageParams.getPageSize()<=0){
            pageParams=new PageParams();
        }
        QueryWrapper<BookCollection> wrapper=new QueryWrapper<>();
        wrapper.eq("username",username);
        //分页对象
        Page<BookCollection> page = new Page<>(pageParams.getPageNo(), pageParams.getPageSize());
        IPage<BookCollection> pageResult = bookCollectionMapper.selectPage(page, wrapper);
        // 获取数据列表
        List<BookCollection> list = pageResult.getRecords();
        // 获取数据总数
        long total = pageResult.getTotal();
        // 构建结果集
        PageResponseResult result=new PageResponseResult((int) pageResult.getCurrent(), (int) pageResult.getSize(), (int) pageResult.getTotal());
        result.setData(list);
        return result;
    }

    @Override
    public R addCollection(AddCollectionDto dto, String username) {
        Integer bId = dto.getBId();
        String title = dto.getTitle();
        if (bId==null||bId<=0||title==null||title.isEmpty()){
            return R.errorResult(500,"参数错误");
        }
        BookCollection bookCollection=new BookCollection();
        bookCollection.setBId(bId);
        bookCollection.setTitle(title);
        bookCollection.setUsername(username);
        List<BookCollection> bookCollections = bookCollectionMapper.selectList(new QueryWrapper<BookCollection>().eq("b_id", bId).eq("title", title).eq("username", username));
        if (bookCollections.size()>0){
            return R.errorResult(500,"已收藏，请勿重复收藏");
        }
        int i = bookCollectionMapper.insert(bookCollection);
        if(i<=0){
            return R.errorResult(500,"收藏失败");
        }
        return R.okResult(200,"收藏成功");
    }

    @Override
    public R deleteCollection(Integer bId, String username) {
        if (bId==null||bId<=0){
            return R.errorResult(500,"参数错误");
        }
        BookCollection bookCollection=new BookCollection();
        QueryWrapper<BookCollection> wrapper=new QueryWrapper<>();
        wrapper.eq("b_id",bId).eq("username",username);
        int i = bookCollectionMapper.delete(wrapper);
        if (i<=0){
            return R.errorResult(500,"取消收藏失败");
        }
        return R.okResult(200,"取消收藏成功");
    }
}
